import { logPageApi } from '@/api/system/log'
import { PageRequest } from '@/api/types'

type props = {
  getDate: () => any
}

export const useLogApi = ({ getDate }: props) => {
  // 获取日志管理列表
  const fetchLogPage = async (pageRequest: PageRequest, queryParams) => {
    try {
      const data = {
        userName: queryParams?.userName,
        requestId: queryParams?.requestId,
        pageRequest
      }
      if (!queryParams?.range) {
        const timeData = getDate()
        Object.assign(data, {
          startDate: timeData.startDate,
          endDate: timeData.endDate
        })
      } else {
        Object.assign(data, {
          startDate: queryParams.range[0],
          endDate: queryParams.range[1]
        })
      }
      const res = await logPageApi(data)

      if (res.code === '0000') {
        return {
          data: res.data?.content,
          preCount: res.data?.preCount,
          nextCount: res.data?.nextCount
        }
      }
    } catch (e) {
      console.log(e)
    }
  }

  return {
    fetchLogPage
  }
}
